
/**
 * Copyright 2009 cestum.org
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
 
package org.waveprotocol.wave.model.id {

/**
 * Comments same as wave-protocol's WaveletId
 * In the context of a single wave, a wavelet is identified by a tuple of a
 * wave provider domain and a local identifier which is unique within the domain
 * and the wave
 */
public final class WaveletId {
	
	private var domain:String;
	private var id:String;
	
	/**
	 * Creates a WaveletId from a serialized wavelet id.
	 */
	public static function deserialise(waveletIdString:String):WaveletId {
		return LongIdSerialiser.INSTANCE.deserialiseWaveletId(waveletIdString);
	}
	public function WaveletId(domain:String, id:String) {
		if (domain == null || id == null) {
			throw new Error("Cannot create WaveletId with null value in [domain:" +
				domain + "] [id:" + id + "]");
		}
		this.domain = domain;
		this.id = id;
	}
	
	public function getDomain():String {
		return domain;
	}
	public function getId():String {
		return id;
	}
	public function serialise():String {
		return LongIdSerialiser.INSTANCE.serialiseWaveletId(this);
	}
	
	public function equals(obj:Object):Boolean {
		if (this == obj) return true;
		if (obj == null) return false;
		var other:WaveletId= WaveletId(obj);
		if (!domain == (other.domain)) return false;
		if (!id == (other.id)) return false;
		return true;
	}
	
	public function toString():String {
		return "[WaveletId:" + serialise() + "]";
	}
	
	public function compareTo(a:Object):int {
		var other:WaveletId=  WaveletId(a);
		var domainCompare:int= domain.localeCompare(other.domain);
		if (domainCompare == 0) {
			return id.localeCompare(other.id);
		} else {
			return domainCompare;
		}
	}
}
}